# -*- coding: utf-8 -*-
# @Time    : 2019/2/20 10:59
# @Author  : yueconger
# @File    : names_update_laws.py
import sys
import os
import re

source_path = r'D:\qq_files\WeChat\WeChat Files\Junjiecongcong\Files\依法分类处理信访诉求清单汇编/'


def read_laws_name():
    file_list = os.listdir(source_path)  # 列出文件夹下所有的目录与文件
    count = 0
    laws_list = []
    laws_list_detail = []

    for i in range(0, len(file_list)):
        path = os.path.join(source_path, file_list[i])
        # 读取文件 寻找带更新法律名称
        with open(path, 'r', encoding='utf-8') as f:
            content = f.read()
        re_rule = '《.*?》'
        re_rule_detail = '《.*?》（.*?年.*?月.*?日.*?）'
        res_list = re.findall(re_rule, content)
        res_list_detail = re.findall(re_rule_detail, content)
        res_list_detail = procee_dtail_law(res_list_detail)

        if len(res_list) > 0:
            res_list = list(set(res_list))
            print(res_list)
        else:
            print('此文件无相关法律')
            res_list = []
        laws_list.extend(res_list)
        laws_list_detail.extend(res_list_detail)
    laws_list = list(set(laws_list))
    laws_list_detail = list(set(laws_list_detail))
    write2file(laws_list, 'laws_name.txt')
    write2file(laws_list_detail, 'laws_name_detail.txt')


def procee_dtail_law(res_list_detail):
    if len(res_list_detail) > 0:
        res_list_detail = list(set(res_list_detail))
    else:
        res_list_detail = []
    return res_list_detail


def write2file(laws_list, path):
    with open(path, 'w', encoding='utf-8') as f:
        for laws in laws_list:
            f.write(laws + '\n')
    print('写入完毕!')


if __name__ == '__main__':
    read_laws_name()
